using UnityEngine;
using System.Collections;

public class GrenadeRobot : Enemy 
{
    public Transform grenadeSpawner;
    public Transform grenadePrefab;
    private Transform target;

    public override void Start()
    {
        base.Start();

        speed = 4;
        hp = 150;
        damage = 20;
        damageDelay = 2;
        damageTimer = 2;

        foreach(Transform children in player.GetComponentsInChildren<Transform>())
        {
            if(children.name == "Personagem")
            {
                target = children;
            }
        }
    }

    public override void Attack()
    {
        base.Attack();

        transform.LookAt(new Vector3(target.position.x, target.position.y, target.position.z));

        int distanceDiff = Mathf.Abs((int)gameObject.transform.position.z - (int)player.position.z);

        if(distanceDiff < 10)
        {
            Shoot();
        }
        else
        {
            transform.Translate((Vector3.forward * speed) * Time.deltaTime);
        }
    }

    public void Shoot()
    {
        damageTimer += Time.deltaTime;

        if(damageTimer >= damageDelay)
        {
            Instantiate(grenadePrefab, new Vector3(grenadeSpawner.transform.position.x, grenadeSpawner.transform.position.y, grenadeSpawner.transform.position.z), transform.rotation);
            damageTimer = 0;
        }
    }
}
